
package com.scalar.chiptrack.sales.dao;


import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.apache.struts.taglib.html.LinkTag;

import com.scalar.chiptrack.sales.Customer;


/**
 * Generate a URL-encoded hyperlink to the specified URI, with
 * associated query parameters selecting a specified Customer.
 *
 * @author Kesava Kumar Achanala
 * @version $Revision: 1.1.1.1 $ $Date: 2006/12/11 23:43:19 $
 */

// Modified by Prabhanjan (07-Jun-2004) to extend LinkTag in order to support even handlers.
//public class LinkCustomerTag extends TagSupport {
public class LinkCustomerTag extends LinkTag {


    // ----------------------------------------------------- Instance Variables


    /**
     * The context-relative URI.
     */
    protected String page = null;



    /**
     * The attribute name.
     */
    private String name = "Customer";


    // ------------------------------------------------------------- Properties


    /**
     * Return the context-relative URI.
     */
    public String getPage() {

	return (this.page);

    }


    /**
     * Set the context-relative URI.
     *
     * @param page Set the context-relative URI
     */
    public void setPage(String page) {

	this.page = page;

    }


    /**
     * Return the attribute name.
     */
    public String getName() {

	return (this.name);

    }


    /**
     * Set the attribute name.
     *
     * @param name The new attribute name
     */
    public void setName(String name) {

	this.name = name;

    }


    // --------------------------------------------------------- Public Methods


    /**
     * Render the beginning of the hyperlink.
     *
     * @exception JspException if a JSP exception has occurred
     */
    public int doStartTag() throws JspException {

		// Generate the URL to be encoded
        HttpServletRequest request =
          (HttpServletRequest) pageContext.getRequest();
        StringBuffer url = new StringBuffer(request.getContextPath());
        url.append(page);

		Customer customer = null;
                String isSample = "1";
		try {
	    	customer = (Customer) pageContext.findAttribute(name);
                isSample = request.getParameter("isSample");

        } catch (ClassCastException e) {
	    	customer = null;
		}
		//replace this with our logs ??????
		//if (customer == null)
	    //throw new JspException
	    //    (messages.getMessage("linkSubscription.noSubscription", name));

		if (page.indexOf("?") < 0)
		    url.append("?");
		else
		    url.append("&");
		url.append("customerId=");
		url.append(customer.getCustomerId());
                url.append("&isSample=");
                url.append(isSample);

		// Generate the hyperlink start element
		HttpServletResponse response =
		  (HttpServletResponse) pageContext.getResponse();
		StringBuffer results = new StringBuffer("<a href=\"");
		results.append(response.encodeURL(url.toString()));
        results.append("\"");
        // Added by Prabhanjan (07-Jun-2004)
        results.append(prepareEventHandlers());
		results.append(">");


		// Print this element to our output writer
		JspWriter writer = pageContext.getOut();
		try {
		    writer.print(results.toString());
		} catch (IOException e) {
			//replace this again with our logs ????
		    //throw new JspException
			//(messages.getMessage("linkSubscription.io", e.toString()));
		}

	// Evaluate the body of this tag
	return (EVAL_BODY_INCLUDE);

    }



    /**
     * Render the end of the hyperlink.
     *
     * @exception JspException if a JSP exception has occurred
     */
    public int doEndTag() throws JspException {


		// Print the ending element to our output writer
		JspWriter writer = pageContext.getOut();
		try {
		    writer.print("</a>");
		} catch (IOException e) {
		    //replace this again with our logs ????
			//throw new JspException
			//(messages.getMessage("linkSubscription.io", e.toString()));
		}

	return (EVAL_PAGE);

    }


    /**
     * Release any acquired resources.
     */
    public void release() {

        super.release();
        this.page = null;
        this.name = "Customer";

    }
}
